package com.android.lmy.GreenDaoDemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lmy.App;
import com.android.lmy.R;
import com.android.lmy.SqlLiteDemo.Student;
import com.android.lmy.SqlLiteDemo.StudentDao;
import com.android.lmy.greendao.BookBeanDao;
import com.android.lmy.greendao.DaoSession;

public class GreenDaoActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private TextView showtext;
    private BookBeanDao bookBeanDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_green_dao);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button5 = findViewById(R.id.button5);
        showtext = findViewById(R.id.showtext);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        bookBeanDao = App.getInstances().getDaoSession().getBookBeanDao();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:
                for (int i = 0; i < 8; i++) {
                    BookBean bookBean = new BookBean();
                    bookBean.setId(i + "");
                    bookBean.setName("小明");
                    bookBean.setNumber("17");
//                    bookBeanDao.insertOrReplace(bookBean);////insertOrReplace  id一样直接替换
                    if (bookBeanDao.insertOrReplace(bookBean) > 0) {
                        Log.e("TTT", "添加成功" + i);
                    }
                }
                break;
            case R.id.button3:
                /**
                 * 删除数据的三种方式
                 *
                 */
//                bookBeanDao.queryBuilder().where(BookBeanDao.Properties.Id.eq(1)).buildDelete().executeDeleteWithoutDetachingEntities();
//                bookBeanDao.deleteByKey("11");//删除id为11的一条数据
                bookBeanDao.deleteAll();
                break;
            case R.id.button4:
                //修改必须要修改数据库里面存在的数据
                BookBean student = bookBeanDao.load("1");
                if (student != null) {
                    student.setNumber("1000");
                    student.setName("我我我我我");
                    student.setPrice("9023");
                    bookBeanDao.update(student);
                } else {
                    Toast.makeText(this, "数据不存在", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.button5:
                if (bookBeanDao.loadAll().size() == 0 || bookBeanDao.loadAll() == null) {
                    Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, bookBeanDao.loadAll().get(0).getName(), Toast.LENGTH_SHORT).show();
                    Log.e("TTT", bookBeanDao.loadAll().toString());
                }
                break;
        }
    }
}


//1：工程的build.gradle
// classpath'org.greenrobot:greendao-gradle-plugin:3.2.2'

// 项目的build.gradle

//  apply plugin:'org.greenrobot.greendao' // apply plugin


//和dependencies同级

//greendao {
//
//        schemaVersion 1
//
//        daoPackage'com.android.lmy.greendao'
//
//        targetGenDir'src/main/java'
//
//        generateTests false
//
//        }


//id 设置尽量避免设置int类型否则删除修改时 报类型转换错误 暂时未知原因
//implementation'org.greenrobot:greendao:3.2.2'